package cn.ronghui.project.domain.application.service.impl;

import cn.ronghui.project.domain.application.adapter.repository.IApplicationRepository;
import cn.ronghui.project.domain.application.model.entity.*;
import cn.ronghui.project.domain.application.service.IApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class ApplicationService implements IApplicationService {

    @Resource
    private IApplicationRepository repository;

    @Override
    public ApplicationResultEntity createApplication(ApplicationCreateEntity applicationCreate) {
        if(applicationCreate == null ||
                applicationCreate.getRoomType() == null ||
                StringUtils.isBlank(applicationCreate.getStudentId()) ||
                applicationCreate.getStartDate() == null ||
                applicationCreate.getEndDate() == null){
            throw new RuntimeException("参数错误");
        }
        log.info("创建申请:{}",applicationCreate);
        ApplicationEntity application = new ApplicationEntity();
        // 1.检查是否重复申请
        application = repository.queryApplicationInfo(applicationCreate.getStudentId());
        if(null !=  application){
            // 1.1 检查分配表中是否存在完成的分配记录
            AllocationEntity allocationResp = new AllocationEntity();
            allocationResp = repository.checkAllocationByStudentId(applicationCreate.getStudentId());
            // 1.2 存在则返回
            if(null != allocationResp){
                log.info("学生：{}已有分配好的房间，{}",applicationCreate.getStudentId(),allocationResp);
                return ApplicationResultEntity.builder()
                        .applicationId(application.getApplicationId())
                        .studentId(application.getStudentId())
                        .roomId(allocationResp.getRoomId())
                        .roomType(application.getRoomType())
                        .status(application.getStatus())
                        .startDate(application.getStartDate())
                        .endDate(application.getEndDate())
                        .price(allocationResp.getPrice())
                        .build();
            }
            // 1.3 不存在则重新创建申请
        }
        // 2.检查申请参数
        // 3.检查学生信息是否存在
        boolean isStudentExist = repository.checkStudentInfo(applicationCreate.getStudentId());
        if(!isStudentExist){
            throw new RuntimeException("学生信息不存在");
        }
        // 4.检查申请房间是否存在，申请时间是否冲突
        boolean isRoomExist = repository.checkRoomInfo(RoomCheckEntity.builder()
                .roomType(applicationCreate.getRoomType())
                .startDate(applicationCreate.getStartDate())
                .endDate(applicationCreate.getEndDate())
                .build());
        if(!isRoomExist){
            throw new RuntimeException("申请房间不存在");
        }
        // 6.创建申请

        return repository.createApplication(applicationCreate);
    }

    @Override
    public ApplicationEntity queryApplicationInfo(Integer applicationId) {
        return null;
    }
}
